package com.hetao.xx.orm;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

public class GoodsDao {
    private final String Tag= getClass().getSimpleName();

    private final Context context;

    public GoodsDao(Context context)  {
        this.context = context;
    }

    public DatabaseHelper getHelper() {
        return DatabaseHelper.getInstance(context);
    }

    public Dao<GoodsVo, Integer> getDao() throws SQLException {
        //传入实体类拿到这个实体类的Dao对象
        return getHelper().getDao(GoodsVo.class);
    }

    public int add(GoodsVo user) {
        try {
            return getDao().create(user);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int update(GoodsVo user) {
        try {
            return getDao().update(user);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }


    public GoodsVo queryByName(String name) {
        try {
            return getDao().queryBuilder().where().eq("name", name).queryForFirst();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public List<GoodsVo> queryAll() {
        try {
            return getDao().queryForAll();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public long countAll() {
        try {
            return getDao().countOf();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int delById(int id) {
        try {
            return getDao().deleteById(id);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int dropTable() {
        try {
            getDao().queryRaw("delete from npc_info");
            getDao().queryRaw("update sqlite_sequence SET seq = 0 where name ='npc_info'");
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }
}
